<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@include file="/ui/jsp/includes/includes.jspf" %> 
<%@include file="/ui/jsp/includes/header.jspf" %>

<script type="text/javascript">
	
	$(document).ready(function () {
		$("#nNumberMatch").hide();
	   
	});

    function addNewStudent()
    {
        $('#addNewStudentForm').attr('action', 'addNewStudent.html');
        $('#addNewStudentForm').submit();
    }
    function updateStudent()
    {
        $('#studentId').val('${student.studentId}');
        $('#addNewStudentForm').attr('action', 'updateStudent.html');
        $('#addNewStudentForm').submit();
    }
    function backToStudentList()
    {
        $('#addNewStudentForm').attr('action', 'viewStudentList.html');
        $('#addNewStudentForm').submit();
    }
    function checkNNumber(){
    	var nNumberMap = '${nNumberMap}';
    	var nNumberStr = nNumberMap.substring(nNumberMap.indexOf('{') + 1, nNumberMap.indexOf('}'));
        nNumberStr = nNumberMap.replace(/=/g, ":");
        var nNumberArr = nNumberStr.split(',');
        var tempArray = new Array();
        var tempStr = "";
        for (var i = 0; i < nNumberArr.length; i++) {
            tempStr = nNumberArr[i];
            tempStr = tempStr.substring(tempStr.indexOf(":") + 1);
            tempArray[i] = tempStr;
        }
    	var nnumber = $("#nnumber").val();
    	var match = false;
    	
    	//alert(tempArray);
    	if(nnumber != ""){
	    	for (var j = 0; j < tempArray.length; j++){
	    		var studentNumber = tempArray[j];
	    		if(nnumber === studentNumber){
	    			match = true;
	    			//alert(studentNumber);
	    		}
	    	}
    	}
    	//alert(match);
    	if(match == true){
    		$("#nNumberMatch").html("This N Number has already been used.");
    		$("#nNumberMatch").show();
    	}
    	else{
    		$("#nNumberMatch").html("");
    		$("#nNumberMatch").hide();
    	}
    }

</script>

<div class="hero-unit secondary_container">
    <div id="table-subject-header">
        <c:if test="${!empty student.nnumber}">
            Update Student
        </c:if>
        <c:if test="${empty student.nnumber}">
            Add New Student
        </c:if>	
    </div>
    <hr>

    <div class="row-fluid">

        <form:form method="POST" id="addNewStudentForm" commandName="student"  class="form-horizontal">
			<form:errors path="*" cssClass="errorblock" element="div"/>
			<div class="errorblock" id="nNumberMatch"></div>
            <div class="control-group">
                <label class="control-label" for="nnumber">N Number*</label>
                <div class="controls">
                    <form:input id="nnumber" path="nnumber" onchange="checkNNumber()"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="firstName">First Name*</label>
                <div class="controls">
                    <form:input id="firstName" path="firstName"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="lastName">Last Name*</label>
                <div class="controls">
                    <form:input id="lastName" path="lastName"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="email">Email*</label>
                <div class="controls">
                    <form:input id="email" path="email"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="major">Major*</label>
                <div class="controls">
                    <form:input id="major" path="major"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="yearGraduated">Year Graduated</label>
                <div class="controls">
                    <form:input id="yearGraduated" path="yearGraduated"/>
                </div>
            </div>
            <div>
              (Leave Year Graduated Blank if Student has not graduated.)\n
              *Required Fields
            </div>
            <div class="control-group">
                <div class="controls">
                    <c:choose>
                        <c:when test="${!empty student.studentId}">
                            <form:hidden path="studentId" id="studentId"/>
                            <input type="button" onclick="javascript:updateStudent()" value="Submit" class="btn btn-primary"/>
                        </c:when>
                        <c:otherwise>
                            <input type="button" onclick="javascript:addNewStudent()" value="Submit" class="btn btn-primary"/>
                        </c:otherwise>
                    </c:choose>
                    <input type="button" value="Cancel" onclick="javascript:backToStudentList()" class="btn btn-primary"/>
                </div>
            </div>
        </form:form>
    </div>
</div>
<%@include file="/ui/jsp/includes/footer.jspf" %>

